# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: xguo

import coin.exchange.base.kr_rest.native_public_client_base as npubcb
import coin.exchange.kraken.kr_rest.constants as exchange_constants


class KrakenNativePublicClient(npubcb.NativePublicClientBase):
  def __init__(self, timeout=1):
    npubcb.NativePublicClientBase.__init__(self, url="https://api.kraken.com", timeout=timeout)
    self.valid_interval = [1, 5, 15, 30, 60, 240, 1440, 10080, 21600, None]

  @staticmethod
  def _assert_success(content):
    assert content is not None
    assert len(content['error']) == 0, content['error']

  def get_server_time(self):
    url = "0/public/Time"
    update = self._get_as_json(url)
    self._assert_success(update.msg)
    return update

  def get_asset_info(self, info=None, aclass=None, asset=None):
    """
    info = info to retrieve (optional):
        info = all info (default)
    aclass = asset class (optional):
        currency (default)
    asset = comma delimited list of assets to get info on (optional.  default = all for given asset class)
    """
    url = "0/public/Assets"
    params = {
        "info": info,
        "aclass": aclass,
        "asset": asset,
    }
    update = self._get_as_json(url, params)
    self._assert_success(update.msg)
    return update

  def get_tradable_asset_pairs(self, info=None, pair=None):
    """
    info = info to retrieve (optional):
        info = all info (default)
        leverage = leverage info
        fees = fees schedule
        margin = margin info
    pair = comma delimited list of asset pairs to get info on (optional.  default = all)
    """
    url = "0/public/AssetPairs"
    params = {
        "info": info,
        "pair": pair,
    }
    update = self._get_as_json(url, params)
    self._assert_success(update.msg)
    return update

  def get_ticker_information(self, pair):
    """
    pair = comma delimited list of asset pairs to get info on
    """
    url = "0/public/Ticker"
    params = {
        "pair": pair,
    }
    update = self._get_as_json(url, params)
    self._assert_success(update.msg)
    return update

  def get_ohlc_data(self, pair, interval=None, since=None):
    """
    pair = asset pair to get OHLC data for
    interval = time frame interval in minutes (optional):
      1 (default), 5, 15, 30, 60, 240, 1440, 10080, 21600
    since = return committed OHLC data since given id (optional.  exclusive)
    """
    url = "0/public/OHLC"
    assert interval in self.valid_interval, "Wrong interval: %s" % interval
    params = {
        "pair": pair,
        "interval": interval,
        "since": since,
    }
    update = self._get_as_json(url, params)
    self._assert_success(update.msg)
    return update

  def get_order_book(self, pair, count=None):
    """
    pair = asset pair to get market depth for
    count = maximum number of asks/bids (optional)
    """
    url = '0/public/Depth'
    params = {
        "pair": pair,
        "count": count,
    }
    update = self._get_as_json(url, params)
    self._assert_success(update.msg)
    return update

  def get_recent_trades(self, pair, since=None):
    """
    pair = asset pair to get trade data for
    since = return trade data since given id (optional.  exclusive)
    """
    url = '0/public/Trades'
    params = {
        "pair": pair,
        "since": since,
    }
    update = self._get_as_json(url, params)
    self._assert_success(update.msg)
    return update

  def get_recent_spread_data(self, pair, since=None):
    """
    pair = asset pair to get spread data for
    since = return spread data since given id (optional.  inclusive)
    """
    url = '0/public/Spread'
    params = {
        "pair": pair,
        "since": since,
    }
    update = self._get_as_json(url, params)
    self._assert_success(update.msg)
    return update

  def get_native_kline(self, pair, interval,  since=None):
    url = '0/public/OHLC'
    params = {
      "pair": pair,
      "interval": interval,
      "since": since,
    }
    update = self._get_as_json(url, params)
    self._assert_success(update.msg)
    return update

  def queryAll(self):
    for symbol in exchange_constants.clean_symbol_list:
      print(self.get_orderbook(symbol))


if __name__ == "__main__":
  client = KrakenNativePublicClient(timeout=10)
  print(client.get_server_time())
  print(client.get_assert_info())
  client.queryAll()
